app.controller('LoginCtrl', ['$rootScope','$localStorage','$scope', '$http','postCfg','$state','userSession', function( $rootScope,$localStorage,$scope, $http,postCfg,$state,userSession) {

    $scope.login = function () {
        if ($scope._form.$invalid) {
            return;
        }
        $http.post($rootScope.settings.path+'/rest/login', {
            username: $scope.username, password: $scope.password
        },postCfg).success(function (response) {
            if (response.resultFlag == 1) {
                $rootScope.settings.setSession(response.data.SESSION);
                $http.get($localStorage.settings.path+'/user').success(function (data) {
                    if(data.authenticated) {
                        userSession.create(data);
                        $state.go('app.dashboard');
                    }else{
                        alert(data.reason);
                    }
                });
            } else {
                alert(response.reason);
            }
        });

    }
}
]);